package com.zprmb.udzt.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Build;
import android.support.annotation.IntegerRes;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.view.View;
import android.view.ViewConfiguration;
import android.widget.OverScroller;

import com.zprmb.udzt.R;

/**
 * 锟斤拷锟斤拷锟�
 */
@SuppressLint("NewApi")
public class RulerView extends View {
    //getSimpleName()锟斤拷锟斤拷源锟斤拷锟斤拷锟叫革拷锟斤拷锟侥底诧拷锟斤拷募锟狡★拷
    final String TAG = RulerView.class.getSimpleName();
    //锟斤拷始锟斤拷围
    private int mBeginRange;
   
    //锟斤拷锟斤拷锟斤拷围
    private int mEndRange;
    /**锟节诧拷锟斤拷龋锟揭诧拷锟斤拷潜锟斤拷每锟斤拷锟侥匡拷锟�*/
    private int mInnerWidth;
    //锟斤拷锟斤拷锟侥匡拷锟侥硷拷锟�
    private int mIndicatePadding;
    //锟斤拷示锟侥伙拷锟斤拷
    private Paint mIndicatePaint;
    //锟斤拷锟街伙拷锟斤拷
    private Paint mTextPaint;
    //锟斤拷示锟侥匡拷锟�
    private int mIndicateWidth;
    //锟斤拷示锟侥达拷小
    private float mIndicateScale;
    //锟斤拷锟斤拷锟斤拷锟狡碉拷X锟斤拷锟斤拷
    private int mLastMotionX;
    /**锟角凤拷锟斤拷曰锟斤拷锟�*/
    private boolean mIsDragged;
    //锟角凤拷锟皆讹拷匹锟斤拷
    private boolean mIsAutoAlign = true;
    //锟角凤拷锟斤拷要锟斤拷示锟斤拷锟斤拷
    private boolean mIsWithText = true;
    //锟斤拷锟斤拷锟斤拷色
    private int mTextColor;
    //锟斤拷锟街达拷小
    private float mTextSize;
    //锟斤拷叩锟斤拷锟缴�
    private int mIndicateColor;
    //锟斤拷小锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷
    private OnScaleListener mListener;
    private OnScaleListener2 mListener2;
    //锟斤拷锟斤拷锟斤拷锟绞撅拷锟轿伙拷茫锟絫op锟斤拷bottom
    private int mGravity;
    /**锟斤拷呔锟斤拷危锟斤拷潭锟斤拷锟斤拷锟�*/
    private Rect mIndicateLoc;
    private int increaseNum;
    /**锟斤拷锟斤拷锟斤拷夭锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟阶帮拷斯锟斤拷锟斤拷氤拷锟斤拷锟斤拷慕锟斤拷锟�*/
    private OverScroller mOverScroller;
    /**锟斤拷锟斤拷锟斤拷锟劫达拷锟斤拷锟铰硷拷锟斤拷锟劫度ｏ拷锟斤拷锟斤拷执锟斤拷投锟斤拷锟饺讹拷锟斤拷锟斤拷*/
    private VelocityTracker mVelocityTracker;
    /**锟斤拷锟斤拷锟斤拷锟�*/
    private int mTouchSlop;
    //锟斤拷小锟斤拷锟斤拷锟斤拷锟斤拷
    private int mMinimumVelocity;
    //锟斤拷锟斤拷锟斤拷锟�
    private int mMaximumVelocity;




    public RulerView(Context context) {
        this(context, null);
    }

    public RulerView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    /**
     * 锟斤拷锟秸讹拷锟角碉拷锟矫此癸拷锟届方锟斤拷
     *
     * @param context
     * @param attrs
     * @param defStyleAttr
     */
    public RulerView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        //锟斤拷取锟皆讹拷锟斤拷锟斤拷锟斤拷锟斤拷锟捷硷拷锟斤拷锟斤拷写锟斤拷缺省值,锟皆讹拷锟斤拷锟斤拷8锟斤拷锟斤拷锟斤拷
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.RulerView);
        mIndicateColor = ta.getColor(R.styleable.RulerView_indicateColor, Color.BLACK);
        mTextColor = ta.getColor(R.styleable.RulerView_textColor_ruler, Color.GRAY);
        mTextSize = ta.getDimension(R.styleable.RulerView_textSize_ruler, 30);
        mBeginRange = ta.getInt(R.styleable.RulerView_begin, 0);
       
        mEndRange = ta.getInt(R.styleable.RulerView_end, 100);
        //锟斤拷呖锟斤拷
        mIndicateWidth = (int) ta.getDimension(R.styleable.RulerView_indicateWidth, 5);
        //锟斤拷叩募锟较�
        mIndicatePadding = (int) ta.getDimension(R.styleable.RulerView_indicatePadding, 5);
        ta.recycle();
        //锟斤拷锟斤拷锟斤拷锟绞撅拷锟轿伙拷茫锟饺笔≈滴拷锟绞撅拷诘撞锟�
        int[] indices = new int[]{android.R.attr.gravity};
        ta = context.obtainStyledAttributes(attrs, indices);
        mGravity = ta.getInt(ta.getIndex(0), Gravity.BOTTOM);
        ta.recycle();
        //默锟斤拷锟斤拷示锟斤拷锟斤拷为0.7锟斤拷
        mIndicateScale = 0.7f;

        initValue();
    }
public void setend(int mEndRange){
	this.mEndRange=mEndRange;
	initValue();
	
}
    /**
     * 锟斤拷始锟斤拷锟斤拷值
     */
    @SuppressLint("NewApi")
	private void initValue() {
        /**  锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷啵拷锟斤拷锟斤拷霉锟斤拷锟侥Ｊ轿拷锟�1.OVER_SCROLL_ALWAYS 锟斤拷准模式
         * 锟斤拷锟斤拷锟斤拷锟街癸拷锟斤拷模式为锟斤拷2.OVER_SCROLL_IF_CONTENT_SCROLLS 锟斤拷锟捷癸拷锟斤拷
         * 3.OVER_SCROLL_NEVER 锟斤拷锟斤拷锟斤拷
         */
        mOverScroller = new OverScroller(getContext());
        setOverScrollMode(OVER_SCROLL_ALWAYS);
        //锟斤拷取锟斤拷图锟斤拷锟矫ｏ拷锟斤拷锟矫达拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟叫★拷锟斤拷锟斤拷拇锟斤拷锟斤拷锟斤拷锟�
        final ViewConfiguration configuration = ViewConfiguration.get(getContext());
        mTouchSlop = configuration.getScaledTouchSlop();
        mMinimumVelocity = configuration.getScaledMinimumFlingVelocity();
        mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();

        //锟斤拷锟矫憋拷叩幕锟斤拷剩锟绞碉拷幕锟�
        mIndicatePaint = new Paint();
        mIndicatePaint.setStyle(Paint.Style.FILL);
        //锟斤拷锟斤拷锟斤拷锟街伙拷锟绞ｏ拷实锟侥伙拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟�
        mTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mTextPaint.setStyle(Paint.Style.FILL);
        mTextPaint.setColor(mTextColor);
        mTextPaint.setTextAlign(Paint.Align.CENTER);
        mTextPaint.setTextSize(mTextSize);
        //锟节诧拷锟斤拷龋锟斤拷锟竭斤拷锟斤拷锟斤拷围-锟斤拷呖锟绞硷拷锟轿э拷锟�*指示锟斤拷锟�
        mInnerWidth = (mEndRange - mBeginRange) * getIndicateWidth();
        //锟斤拷叨锟轿晃伙拷锟斤拷锟斤拷锟�
        mIndicateLoc = new Rect();


    }



    /**
     * 锟斤拷写锟斤拷锟狡凤拷锟斤拷
     *
     * @param canvas
     */
    @Override
    protected void onDraw(Canvas canvas) {

        /**
         * 锟斤拷锟斤拷锟角对伙拷锟斤拷锟斤拷锟斤拷锟斤拷转锟斤拷锟斤拷锟脚ｏ拷平锟狡等诧拷锟斤拷锟斤拷时锟斤拷锟斤拷实锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷囟锟斤拷锟皆拷亟锟斤拷胁锟斤拷锟斤拷锟�
         * 锟斤拷锟斤拷图片锟斤拷一锟斤拷锟斤拷锟轿等ｏ拷锟斤拷锟角碉拷锟斤拷锟斤拷canvas锟侥凤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷些锟斤拷锟斤拷锟斤拷时锟斤拷
         * 锟斤拷实锟角讹拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟剿诧拷锟斤拷锟斤拷锟斤拷么之锟斤拷锟节伙拷锟斤拷锟较碉拷元锟截讹拷锟斤拷锟杰碉拷影锟届，
         * 锟斤拷锟斤拷锟斤拷锟斤拷锟节诧拷锟斤拷之前锟斤拷锟斤拷canvas.save()锟斤拷锟斤拷锟芥画锟斤拷锟斤拷前锟斤拷状态锟斤拷
         * 锟斤拷锟斤拷锟斤拷之锟斤拷取锟斤拷之前锟斤拷锟斤拷锟斤拷锟阶刺拷锟斤拷锟斤拷锟斤拷筒锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷元锟截斤拷锟斤拷影锟斤拷
         */
        int count = canvas.save();
        //循锟斤拷锟斤拷锟狡憋拷锟斤拷锟斤拷锟斤拷潭龋锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷值锟斤拷锟斤拷小值锟斤拷锟斤拷锟斤拷
        for (int value = mBeginRange, position = 0; value <= mEndRange; value++, position++) {
            drawIndicate(canvas, position);
            //锟斤拷锟斤拷锟揭拷锟斤拷郑锟斤拷锟斤拷锟揭拷诳潭锟斤拷禄锟斤拷锟斤拷锟斤拷锟�
            if (mIsWithText)
                drawText(canvas, position, String.valueOf(value*increaseNum));
           
        }

        //锟街革拷Canvas锟斤拷状态
        canvas.restoreToCount(count);

    }
  public void setTextIncreaseNum(int increaseNum){
	  this.increaseNum=increaseNum;
  }
    /**
     * 锟斤拷锟狡憋拷锟斤拷锟斤拷锟斤拷潭龋锟斤拷锟�0锟斤拷100锟酵伙拷锟斤拷示100锟斤拷锟教讹拷
     * @param canvas 锟斤拷锟斤拷
     * @param position
     */
    private void drawIndicate(Canvas canvas, int position) {
        computeIndicateLoc(mIndicateLoc, position);
        int left = mIndicateLoc.left + mIndicatePadding;
        int right = mIndicateLoc.right - mIndicatePadding;
        int top = mIndicateLoc.top;
        int bottom = mIndicateLoc.bottom;

        if (position % 5 != 0) {
            int indicateHeight = bottom - top;
            if (isAlignTop()) {
                bottom = (int) (top + indicateHeight * mIndicateScale);
            } else {
                top = (int) (bottom - indicateHeight * mIndicateScale);
            }
        }

        mIndicatePaint.setColor(mIndicateColor);
        canvas.drawRect(left, top, right, bottom, mIndicatePaint);
        canvas.drawLine(left,0,right,0,mIndicatePaint);
    }

    /**
     * 锟斤拷锟斤拷锟斤拷锟街ｏ拷每5锟斤拷锟教度伙拷锟斤拷一锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷示
     * @param canvas
     * @param position
     * @param text
     */
    private void drawText(Canvas canvas, int position, String text) {
        if (position % 10 != 0)
            return;

        computeIndicateLoc(mIndicateLoc, position);
        int textHeight = computeTextHeight();

        mTextPaint.setColor(mTextColor);
        mTextPaint.setTextSize(mTextSize);
        mTextPaint.setTextAlign(Paint.Align.CENTER);

        int x = (mIndicateLoc.left + mIndicateLoc.right) / 2;
        int y = mIndicateLoc.bottom + textHeight;

        if (!isAlignTop()) {
            y = mIndicateLoc.top;
            mTextPaint.getTextBounds(text, 0, text.length(), mIndicateLoc);
            y += mIndicateLoc.top / 2;  //锟斤拷锟斤拷一些偏锟斤拷
        }

        canvas.drawText(text, x, y, mTextPaint);

    }

    /**
     * 锟斤拷锟斤拷指示锟斤拷锟斤拷位锟矫ｏ拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷
     * 锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟剿此撅拷锟轿ｏ拷锟教度碉拷锟斤拷锟斤拷锟斤拷锟铰ｏ拷
     * @param outRect 锟斤拷锟斤拷
     * @param position 位锟斤拷锟斤拷值锟斤拷锟斤拷锟斤拷诩锟斤拷锟斤拷潭龋锟�
     */
    private void computeIndicateLoc(Rect outRect, int position) {
        if (outRect == null)
            return;

        int height = getHeight();
        int indicate = getIndicateWidth();

        int left = (indicate * position);
        int right = left + indicate;
        int top = getPaddingTop();//锟斤拷玫锟角癡iew锟侥讹拷锟节撅拷
        int bottom = height - getPaddingBottom();//锟斤拷图锟竭讹拷-锟斤拷图锟斤拷锟节撅拷

        if (mIsWithText) {
            int textHeight = computeTextHeight();
            if (isAlignTop())
                bottom -= textHeight;//锟斤拷锟斤拷强潭锟斤拷锟绞撅拷诙锟斤拷锟斤拷锟斤拷撞锟揭拷锟饺ワ拷锟斤拷值母叨锟�
            else
                top += textHeight;//锟斤拷锟斤拷强潭锟斤拷锟绞撅拷诘撞锟斤拷锟斤拷锟斤拷锟揭拷锟斤拷锟斤拷锟斤拷值母叨锟�
        }
        //锟斤拷锟斤拷偏锟斤拷锟斤拷锟斤拷锟斤拷吆锟斤拷冶叨锟斤拷锟斤拷锟揭伙拷锟狡拷锟斤拷锟�
        int offsets = getStartOffsets();
        left += offsets;
        right += offsets;
        outRect.set(left, top, right, bottom);
    }

    /**
     * 锟斤拷始偏锟狡ｏ拷锟斤拷锟揭拷锟斤拷锟斤拷锟斤拷值幕锟斤拷锟斤拷锟揭拷啤锟�
     *
     * @return
     */
    private int getStartOffsets() {
        if (mIsWithText) {
            String text = String.valueOf(mBeginRange);
            //锟斤拷锟斤拷锟斤拷锟街的匡拷锟�
            int textWidth = (int) mTextPaint.measureText(text, 0, text.length());
            return textWidth / 2;//实锟斤拷偏锟斤拷锟斤拷锟街匡拷鹊锟揭伙拷耄癸拷锟斤拷锟斤拷锟斤拷示
        }
        return 0;
    }

    /**
     * 锟斤拷锟斤拷锟斤拷锟斤拷录锟�
     * @param event
     * @return
     */
    @SuppressLint("NewApi")
	@Override
    public boolean onTouchEvent(MotionEvent event) {

        //锟斤拷锟斤拷锟斤拷锟斤拷诔锟绞硷拷俣雀锟斤拷锟�
        initVelocityTrackerIfNotExists();
        //锟劫讹拷追锟斤拷锟斤拷 锟斤拷锟斤拷贫锟斤拷录锟�
        mVelocityTracker.addMovement(event);
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                //锟斤拷锟斤拷时锟斤拷锟斤拷锟斤拷锟斤拷锟矫伙拷锟斤拷锟�
                if (mIsDragged = !mOverScroller.isFinished()) {
                    if (getParent() != null)
                        //要锟斤拷锟街癸拷锟斤拷卮锟斤拷锟斤拷录锟�
                        getParent().requestDisallowInterceptTouchEvent(true);
                }
                //锟斤拷锟斤拷锟斤拷锟矫伙拷锟斤拷锟斤拷锟斤拷徒锟斤拷锟斤拷锟斤拷锟�
                if (!mOverScroller.isFinished())
                    mOverScroller.abortAnimation();
                //锟斤拷录锟斤拷锟铰碉拷x锟斤拷锟斤拷锟斤拷
                mLastMotionX = (int) event.getX();

                return true;

            case MotionEvent.ACTION_MOVE:
                //锟狡讹拷时x锟斤拷值锟斤拷锟斤拷锟矫碉拷锟斤拷锟斤拷锟斤拷x值-锟狡讹拷x锟斤拷值锟侥诧拷值
                int curX = (int) event.getX();
                int deltaX = mLastMotionX - curX;
                //锟斤拷锟斤拷锟斤拷锟轿达拷锟斤拷锟斤拷锟斤拷锟斤拷贫锟斤拷锟斤拷锟侥撅拷锟斤拷值锟斤拷锟节达拷锟斤拷锟斤拷锟斤拷锟�
                if (!mIsDragged && Math.abs(deltaX) > mTouchSlop) {
                    if (getParent() != null)
                        //锟斤拷锟斤拷懈锟斤拷锟斤拷丶锟斤拷锟斤拷透锟斤拷吒锟斤拷锟斤拷丶锟斤拷锟揭拷锟斤拷锟斤拷业拇锟斤拷锟斤拷录锟�
                        getParent().requestDisallowInterceptTouchEvent(true);
                    //锟斤拷锟斤拷锟矫伙拷锟斤拷锟斤拷锟斤拷
                    mIsDragged = true;
                    //锟斤拷锟斤拷锟斤拷锟斤拷锟街碉拷锟�0锟斤拷锟斤拷锟斤拷锟斤拷值锟斤拷要-锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷
                    if (deltaX > 0) {
                        deltaX -= mTouchSlop;
                    } else {
                        deltaX += mTouchSlop;
                    }
                }
                //锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟絰锟斤拷锟斤拷锟斤拷堑锟角帮拷锟斤拷锟斤拷牡牡锟�
                if (mIsDragged) {
                    mLastMotionX = curX;
                    //锟斤拷锟斤拷锟斤拷锟斤拷锟絏值锟斤拷0锟斤拷锟竭达拷锟斤拷锟斤拷锟侥癸拷锟斤拷值锟剿ｏ拷锟矫达拷锟斤拷锟斤拷值*0.7
                    if (getScrollX() <= 0 || getScrollX() >= getMaximumScroll())
                        deltaX *= 0.7;
                    //锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟侥憋拷准锟斤拷为锟斤拷锟斤拷图,锟斤拷锟绞硷拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟�
                    if (overScrollBy(deltaX, 0, getScrollX(), getScrollY(), getMaximumScroll(), 0, getWidth(), 0, true)) {
                        mVelocityTracker.clear();
                    }

                }

                break;
            case MotionEvent.ACTION_UP: {
                if (mIsDragged) {
                    //锟斤拷榛拷锟斤拷锟斤拷俣龋锟�1000锟斤拷位
                    mVelocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
                    //锟斤拷锟絏锟斤拷锟较碉拷锟斤拷锟斤拷
                    int initialVelocity = (int) mVelocityTracker.getXVelocity();
                    //锟斤拷锟絰锟斤拷锟斤拷锟劫★拷锟斤拷小锟斤拷锟斤拷
                    if ((Math.abs(initialVelocity) > mMinimumVelocity)) {
                        fling(-initialVelocity);
                    } else {
                        //alignCenter();
                        //锟截碉拷锟斤拷末尾
                        sprintBack();
                    }
                }
                //锟斤拷锟斤拷锟斤拷锟斤拷
                mIsDragged = false;
                //锟酵凤拷追锟斤拷锟斤拷锟斤拷源
                recycleVelocityTracker();
                break;
            }
            case MotionEvent.ACTION_CANCEL: {
                //锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷夜锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷突毓锟�
                if (mIsDragged && mOverScroller.isFinished()) {
                    sprintBack();
                }

                mIsDragged = false;

                recycleVelocityTracker();
                break;
            }
        }

        return true;
    }

    /**
     * 刷锟铰诧拷锟斤拷值
     */
    private void refreshValues() {
        //锟节诧拷锟斤拷锟� = 锟斤拷锟斤拷锟街�-锟斤拷始值锟斤拷*锟教度匡拷锟�
        mInnerWidth = (mEndRange - mBeginRange) * getIndicateWidth();
        invalidateView();

    }

    /**
     * 锟斤拷锟斤拷指示锟斤拷锟� 锟斤拷锟教度匡拷锟�+锟教讹拷锟节边撅拷+锟教讹拷锟节边撅拷
     *
     * @return
     */
    private int getIndicateWidth() {
        return mIndicateWidth + mIndicatePadding + mIndicatePadding;
        
    }

    /**
     * 锟斤拷取锟斤拷小锟斤拷锟斤拷值锟斤拷
     *
     * @return
     */
    private int getMinimumScroll() {
        return -(getWidth() - getIndicateWidth()) / 2 + getStartOffsets();
    }

    /**
     * 锟斤拷取锟斤拷锟斤拷锟斤拷值锟斤拷
     *
     * @return
     */
    private int getMaximumScroll() {
        return mInnerWidth + getMinimumScroll();
    }

    /**
     * 锟斤拷锟斤拷锟教度ｏ拷使锟斤拷锟斤拷小锟�
     */
    private void adjustIndicate() {
        if (!mOverScroller.isFinished())
            mOverScroller.abortAnimation();

        int position = computeSelectedPosition();
        int scrollX = getScrollByPosition(position);
        scrollX -= getScrollX();

        if (scrollX != 0) {
            //锟斤拷锟斤拷锟竭界开始锟斤拷锟斤拷
            mOverScroller.startScroll(getScrollX(), getScrollY(), scrollX, 0);
            invalidateView();
        }
    }

    /**
     * 投锟斤拷
     * @param velocityX
     * 锟斤拷锟斤拷x锟结滑锟斤拷锟斤拷锟绞ｏ拷锟斤拷锟斤拷锟斤拷刷锟铰斤拷锟斤拷
     */
    @SuppressLint("NewApi")
	public void fling(int velocityX) {
        mOverScroller.fling(getScrollX(), getScrollY(), velocityX, 0, getMinimumScroll(), getMaximumScroll(), 0, 0, getWidth() / 2, 0);
        invalidateView(); 
    }

    /**
     * 锟截碉拷
     */
    public void sprintBack() {
        mOverScroller.springBack(getScrollX(), getScrollY(), getMinimumScroll(), getMaximumScroll(), 0, 0);
        invalidateView();
    }


    public void setOnScaleListener(OnScaleListener listener) {
        if (listener != null) {
            mListener = listener;
        }
    }
    public void setOnScaleListener2(OnScaleListener2 listener) {
        if (listener != null) {
            mListener2 = listener;
        }
    }
    /**
     * 锟斤拷取position锟侥撅拷锟皆癸拷锟斤拷位锟矫★拷
     *
     * @param position
     * @return
     */
    private int getScrollByPosition(int position) {
        computeIndicateLoc(mIndicateLoc, position);
        int scrollX = mIndicateLoc.left - getStartOffsets() + getMinimumScroll();
        return scrollX;
    }

    /**
     * 锟斤拷锟姐当前锟斤拷选锟斤拷锟轿伙拷锟�
     *
     * @return
     */
    public int computeSelectedPosition() {
        //锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷潭鹊锟斤拷屑锟轿伙拷锟�
        int centerX = getScrollX() - getMinimumScroll() + getIndicateWidth() / 2;
        //通锟斤拷锟叫硷拷位锟斤拷锟斤拷锟叫讹拷选锟斤拷目潭锟街滴伙拷锟�
        centerX = Math.max(0, Math.min(mInnerWidth, centerX));
        int position = centerX / getIndicateWidth();
        return position;
    }

    /**
     * 平锟斤拷锟斤拷锟斤拷
     * @param position
     */
    public void smoothScrollTo(int position) {
        //锟斤拷锟窖★拷锟斤拷位锟斤拷<0锟斤拷锟竭匡拷始值+选锟斤拷位锟矫达拷锟斤拷锟斤拷锟斤拷值锟斤拷锟斤拷直锟接凤拷锟截帮拷
        if (position < 0 || mBeginRange + position > mEndRange)
            return;
        //锟斤拷锟斤拷锟斤拷锟矫伙拷锟斤拷锟缴ｏ拷锟叫讹拷锟斤拷锟侥讹拷锟斤拷锟斤拷
        if (!mOverScroller.isFinished())
            mOverScroller.abortAnimation();

        int scrollX = getScrollByPosition(position);
        mOverScroller.startScroll(getScrollX(), getScrollY(), scrollX - getScrollX(), 0);
        invalidateView();
    }

    /**
     * 平锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷值
     * @param value
     */
    public void smoothScrollToValue(int value) {
        int position = value - mBeginRange;
        smoothScrollTo(position);
    }

    /**
     * 锟斤拷锟斤拷锟脚达拷锟斤拷小锟铰硷拷
     * @param scale
     */
    private void onScaleChanged(int scale) {
        if (mListener != null)
            mListener.onScaleChanged(scale);
    }

    /**
     * 锟斤拷锟斤拷锟节癸拷锟斤拷时锟斤拷锟铰硷拷
     * @param scrollX
     * @param scrollY
     * @param clampedX 锟教讹拷锟斤拷x
     * @param clampedY 锟教讹拷锟斤拷Y
     */
    public int getscale(){
    	int position = computeSelectedPosition();
       
    	return position + mBeginRange;
    }
    @Override
    protected void onOverScrolled(int scrollX, int scrollY, boolean clampedX, boolean clampedY) {
        //锟斤拷锟斤拷锟斤拷锟矫伙拷锟斤拷锟缴ｏ拷锟斤拷锟矫癸拷锟斤拷x锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷
        if (!mOverScroller.isFinished()) {
            final int oldX = getScrollX();
            final int oldY = getScrollY();
            setScrollX(scrollX);
            onScrollChanged(scrollX, scrollY, oldX, oldY);
            if (clampedX) {
                //sprintBack();
            }
        } else {
            super.scrollTo(scrollX, scrollY);
        }
        //锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟轿猲ull锟斤拷锟斤拷值锟斤拷前选锟斤拷锟轿伙拷茫锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷鸥谋锟斤拷录锟�
        if (mListener != null) {
            int position = computeSelectedPosition();
            onScaleChanged(position + mBeginRange);
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);


    }

    /**
     * 锟斤拷锟斤拷锟斤拷锟街高讹拷
     * @return
     */
    private int computeTextHeight() {
        //使锟斤拷FontMetrics锟斤拷锟襟，硷拷锟斤拷锟斤拷锟街碉拷锟斤拷锟疥。
        Paint.FontMetrics fontMetrics = mTextPaint.getFontMetrics();
        float textHeight = fontMetrics.descent - fontMetrics.ascent;
        return (int) textHeight;
    }

    private boolean isAlignTop() {
        //&为位锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟�32位锟斤拷锟斤拷锟斤拷值锟矫比斤拷
        return (mGravity & Gravity.TOP) == Gravity.TOP;
    }

    public void setGravity(int gravity) {
        this.mGravity = gravity;
        invalidateView();
    }

    /**
     * 锟斤拷锟斤拷锟斤拷锟�
     */
    @Override
    public void computeScroll() {
        if (mOverScroller.computeScrollOffset()) {
            int oldX = getScrollX();
            int oldY = getScrollY();
           // 锟斤拷锟截癸拷锟斤拷锟叫的碉拷锟斤拷偏锟斤拷锟斤拷,锟劫度撅拷然锟斤拷么锟斤拷锟斤拷
            int x = mOverScroller.getCurrX();
            int y = mOverScroller.getCurrY();
            //锟斤拷锟斤拷锟斤拷锟斤拷貌锟斤拷锟�
            overScrollBy(x - oldX, y - oldY, oldX, oldY, getMaximumScroll(), 0, getWidth(), 0, false);
            invalidateView();
        } else if (!mIsDragged && mIsAutoAlign) {//锟斤拷锟斤拷锟斤拷俟锟斤拷锟斤拷铱锟斤拷锟斤拷锟斤拷远锟斤拷锟斤拷锟�
            adjustIndicate();
        }
    }

    @Override
    protected int computeHorizontalScrollRange() {
        return getMaximumScroll();
    }

    /**
     * 刷锟铰斤拷锟斤拷
     * 锟斤拷锟斤拷姹撅拷锟斤拷锟�16锟斤拷4.1锟斤拷
     * 使锟斤拷postInvalidate锟斤拷锟斤拷直锟斤拷锟斤拷锟竭筹拷锟叫革拷锟铰斤拷锟斤拷
     * invalidate()锟斤拷锟斤拷锟斤拷UI锟竭筹拷锟斤拷使锟斤拷
     */
    public void invalidateView() {
        if (Build.VERSION.SDK_INT >= 16) {
            postInvalidateOnAnimation();
        } else
            invalidate();
    }

    /**
     * 锟斤拷锟斤拷锟阶拷锟阶凤拷锟斤拷锟�
     */
    @SuppressLint("NewApi")
	private void initVelocityTrackerIfNotExists() {               
        if (mVelocityTracker == null) {
            //锟斤拷玫锟角帮拷锟阶拷锟阶凤拷锟�
            mVelocityTracker = VelocityTracker.obtain();
        }
    }
    /**
     * 锟酵凤拷 锟斤拷转锟斤拷追锟斤拷锟斤拷锟斤拷源
     */
    private void recycleVelocityTracker() {
        if (mVelocityTracker != null) {
            mVelocityTracker.recycle();
            mVelocityTracker = null;
        }
    }

    /**
     * 锟脚达拷锟斤拷小锟斤拷锟斤拷锟接匡拷
     */
    public interface OnScaleListener {
        void onScaleChanged(int scale);

    }
    public interface OnScaleListener2 {
        

    }
    /**
     * 锟斤拷锟矫刻度的匡拷锟�
     * @param indicateWidth
     */
    public void setIndicateWidth(@IntegerRes int indicateWidth) {
        this.mIndicateWidth = indicateWidth;
        refreshValues();
    }

    /**
     * 锟斤拷锟矫刻讹拷锟节硷拷锟�
     * @param indicatePadding
     */
    public void setIndicatePadding(@IntegerRes int indicatePadding) {
        this.mIndicatePadding = indicatePadding;
        refreshValues();
    }

    public void setWithText(boolean withText) {
        this.mIsWithText = withText;
        refreshValues();
    }

    public void setAutoAlign(boolean autoAlign) {
        this.mIsAutoAlign = autoAlign;
        refreshValues();
    }

    /**
     * 锟角凤拷锟斤拷示锟斤拷锟斤拷
     * @return
     */
    public boolean isWithText() {
        return mIsWithText;
    }

    /**
     * 锟皆讹拷锟斤拷锟斤拷潭锟�
     * @return
     */
    public boolean isAutoAlign() {
        return mIsAutoAlign;
    }
}